* ---------------------------------------------
* ' test individual vs contextual vs minority-majority index
* ' across different levels
* ---------------------------------------------

global home_dir "/N/project/suicide_study/pnas_replication"

local model_version "0"
local geo_type "county"
local subgroup "all" 
local year "all" 
local model_interact "main"
local interaction "sameness"

global figure_file "${home_dir}/results/log"

cd ${home_dir}

log using "${figure_file}/table_appendix_0819.txt", replace text

* run ONCE the following lines to collect data
*if ("`model'" == "logit"){
*	use "${home_dir}/data/processed/suicide_reg_county_v1.dta", clear 
*	merge 1:1 uid Year county using "${home_dir}/data/processed/suicide_reg_R_v1.dta", gen(merge_R)
*	compress
*	save "${home_dir}/data/processed/suicide_reg_v1_raw.dta", replace
*}
*
*if ("`model'" == "mi") {
*	
*	cap: confirm file "${home_dir}/data/processed/suicide_reg_v1_imputed_M10.dta"
*
*	if ( _rc == 0 ) {
*	
*		use "${home_dir}/data/processed/suicide_reg_v1_imputed_M10.dta", clear 
*	
*	} 
*	else {
*
*		use "${home_dir}/data/processed/suicide_reg_county_v1.dta", clear 
*		replace RAT_UnEmpl = . if missing(UnEmpl)
*		replace std_same_prop_UnEmpl = . if missing(UnEmpl)
*		replace RAT_PhysProb = . if missing(PhysProb)
*		replace std_same_prop_PhysProb = . if missing(PhysProb)
*		gen mm = 0
*		
*		forvalues yy = 2005/2011 {
*			append using "${home_dir}/data/processed/suicide_reg_Y`yy'_v1.dta"
*		}
*		compress
*		merge m:1 uid Year county using "${home_dir}/data/processed/suicide_reg_R_v1.dta", gen(merge_R)
*		keep if merge_R == 3
*		drop merge_R
*		compress
*
*		mi import flong, clear m(mm) id(uid county Year) imputed(MarStat5 UnEmpl PhysProb std_same_prop_MarStat5 std_same_prop_UnEmpl std_same_prop_PhysProb)
*
*		save "${home_dir}/data/processed/suicide_reg_v1_imputed_M10.dta", replace
*	}
*}

* parameter set-up for regression modeling
local religion Rat_GC_ProE Rat_GC_ProM Rat_GC_ProB Rat_GC_Jew Rat_GC_Oth
local contextual_control Rat_Poverty Rat_Mig_Cum Pop_Den

local demographics_raw i.Female c.RAT_Female i.AgeGrp4 c.RAT_AgeGrp4_2 c.RAT_AgeGrp4_3 c.RAT_AgeGrp4_4 i.Race5 c.RAT_Race5_2 c.RAT_Race5_3 c.RAT_Race5_4 c.RAT_Race5_5 i.BornUSA c.RAT_BornUSA i.MarStat5 c.RAT_MarStat5_2 c.RAT_MarStat5_3 c.RAT_MarStat5_4 c.RAT_MarStat5_5
local demographics_same i.Female c.std_same_prop_Sex i.AgeGrp4 c.std_same_prop_AgeGrp4 i.Race5 c.std_same_prop_Race5 i.BornUSA c.std_same_prop_BornUSA i.MarStat5 c.std_same_prop_MarStat5 
local demographics_inter i.Female##c.std_same_prop_Sex i.AgeGrp4##c.std_same_prop_AgeGrp4 i.Race5##c.std_same_prop_Race5 i.BornUSA##c.std_same_prop_BornUSA i.MarStat5##c.std_same_prop_MarStat5

local main_model_eq1 i.Year `demographics_raw' `contextual_control' `religion' 
local main_model_eq2 i.Year `demographics_raw' `contextual_control' `religion' i.UnEmpl c.RAT_UnEmpl i.PhysProb c.RAT_PhysProb

local main_model_eq3 i.Year `demographics_same' `contextual_control' `religion' 
local main_model_eq4 i.Year `demographics_same' `contextual_control' `religion' i.UnEmpl c.std_same_prop_UnEmpl i.PhysProb c.std_same_prop_PhysProb

local main_model_eq5 i.Year `demographics_inter' `contextual_control' `religion' 
local main_model_eq6 i.Year `demographics_inter' `contextual_control' `religion' i.UnEmpl##c.std_same_prop_UnEmpl i.PhysProb##c.std_same_prop_PhysProb

* first logit model
use "${home_dir}/data/processed/suicide_reg_v1_raw.dta", clear
svyset `geo_type' [pw=ObsWgt0] 	

* recode some measures for dummy coding
tab Race5, gen(race5_nh)
	rename race5_nh1 White_nh 
	rename race5_nh2 Black_nh 
	rename race5_nh3 AIAN_nh 
	rename race5_nh4 AsPI_nh 
	rename race5_nh5 Hispanic

tab MarStat5, gen(ms)
	rename ms1 Marrd5
	rename ms2 Widow5
	rename ms3 Divor5
	rename ms4 Separ5
	rename ms5 NvMar5

tab AgeGrp4, gen(ag) 
	rename ag1 Age_15_24
	rename ag2 Age_25_44
	rename ag3 Age_45_64
	rename ag4 Age_65_Up

destring St, replace 

* for descriptive tables : sample characteristics for all 
egen n_miss = rowmiss(Female White_nh Black_nh AIAN_nh AsPI_nh Hispanic BornUSA Age_15_24 Age_25_44 Age_45_64 Age_65_Up Marrd5 Widow5 Divor5 Separ5 NvMar5 Pop_Den Rat_Mig_Cum Rat_GC_ProE Rat_GC_ProM Rat_GC_ProB Rat_GC_Jew Rat_GC_Oth)

tab n_miss Suic,col 

* general over-time suicide rates
mean Suic [aw=ObsWgt0] if n_miss == 0
mean Suic [aw=ObsWgt0] if n_miss == 0, over(Year)

* Table S1 
sum Suic Female White_nh Black_nh AIAN_nh AsPI_nh Hispanic BornUSA Age_15_24 Age_25_44 Age_45_64 Age_65_Up Marrd5 Widow5 Divor5 Separ5 NvMar5 PhysProb UnEmpl RAT_* Rat_Poverty Rat_Mig_Cum Pop_Den Rat_GC_ProE Rat_GC_ProM Rat_GC_ProB Rat_GC_Cath Rat_GC_Jew Rat_GC_Oth [aw=ObsWgt0] if Suic == 1 & n_miss == 0
sum Suic Female White_nh Black_nh AIAN_nh AsPI_nh Hispanic BornUSA Age_15_24 Age_25_44 Age_45_64 Age_65_Up Marrd5 Widow5 Divor5 Separ5 NvMar5 PhysProb UnEmpl RAT_* Rat_Poverty Rat_Mig_Cum Pop_Den Rat_GC_ProE Rat_GC_ProM Rat_GC_ProB Rat_GC_Cath Rat_GC_Jew Rat_GC_Oth [aw=ObsWgt0] if Suic == 0 & n_miss == 0
sum Suic Female White_nh Black_nh AIAN_nh AsPI_nh Hispanic BornUSA Age_15_24 Age_25_44 Age_45_64 Age_65_Up Marrd5 Widow5 Divor5 Separ5 NvMar5 PhysProb UnEmpl RAT_* Rat_Poverty Rat_Mig_Cum Pop_Den Rat_GC_ProE Rat_GC_ProM Rat_GC_ProB Rat_GC_Cath Rat_GC_Jew Rat_GC_Oth [aw=ObsWgt0] if n_miss == 0

* Table S3
sum Suic Female White_nh Black_nh AIAN_nh AsPI_nh Hispanic BornUSA Age_15_24 Age_25_44 Age_45_64 Age_65_Up Marrd5 Widow5 Divor5 Separ5 NvMar5 PhysProb UnEmpl [aw=ObsWgt0] if n_miss > 0 & Suic == 1 



* Table S4 - S6 Model 1
estimates clear
svy: logit Suic i.St `main_model_eq1' 	
estimates store m1 
svy: logit Suic i.St `main_model_eq2' 	
estimates store m2 
svy: logit Suic i.St `main_model_eq3' 	
estimates store m3 
svy: logit Suic i.St `main_model_eq4' 	
estimates store m4 
svy: logit Suic i.St `main_model_eq5' 	
estimates store m5 
svy: logit Suic i.St `main_model_eq6' 	
estimates store m6 

esttab m1 m2 m3 m4 m5 m6 using "${figure_file}/logit_appendix_table.csv", csv replace star (+ 0.1 * 0.05 ** 0.01 *** 0.001) label nogap se 

* multiple imputation parts
use "${home_dir}/data/processed/suicide_reg_v1_imputed_M10.dta", clear 
mi svyset `geo_type' [pw=ObsWgt0] 


* recode some measures for dummy coding
tab Race5, gen(race5_nh)
	rename race5_nh1 White_nh 
	rename race5_nh2 Black_nh 
	rename race5_nh3 AIAN_nh 
	rename race5_nh4 AsPI_nh 
	rename race5_nh5 Hispanic

tab MarStat5, gen(ms)
	rename ms1 Marrd5
	rename ms2 Widow5
	rename ms3 Divor5
	rename ms4 Separ5
	rename ms5 NvMar5

tab AgeGrp4, gen(ag) 
	rename ag1 Age_15_24
	rename ag2 Age_25_44
	rename ag3 Age_45_64
	rename ag4 Age_65_Up

destring St, replace 

* for descriptive tables : sample characteristics for all 
egen n_miss = rowmiss(Female White_nh Black_nh AIAN_nh AsPI_nh Hispanic BornUSA Age_15_24 Age_25_44 Age_45_64 Age_65_Up Marrd5 Widow5 Divor5 Separ5 NvMar5 Pop_Den Rat_Mig_Cum)

local religion Rat_GC_ProE Rat_GC_ProM Rat_GC_ProB Rat_GC_Jew Rat_GC_Oth
local contextual_control Rat_Poverty Rat_Mig_Cum Pop_Den

local demographics_raw i.Female c.RAT_Female i.AgeGrp4 c.RAT_AgeGrp4_2 c.RAT_AgeGrp4_3 c.RAT_AgeGrp4_4 i.Race5 c.RAT_Race5_2 c.RAT_Race5_3 c.RAT_Race5_4 c.RAT_Race5_5 i.BornUSA c.RAT_BornUSA i.MarStat5 c.RAT_MarStat5_2 c.RAT_MarStat5_3 c.RAT_MarStat5_4 c.RAT_MarStat5_5
local demographics_same i.Female c.std_same_prop_Sex i.AgeGrp4 c.std_same_prop_AgeGrp4 i.Race5 c.std_same_prop_Race5 i.BornUSA c.std_same_prop_BornUSA i.MarStat5 c.std_same_prop_MarStat5 
local demographics_inter i.Female##c.std_same_prop_Sex i.AgeGrp4##c.std_same_prop_AgeGrp4 i.Race5##c.std_same_prop_Race5 i.BornUSA##c.std_same_prop_BornUSA i.MarStat5##c.std_same_prop_MarStat5

local main_model_eq1 i.Year `demographics_raw' `contextual_control' `religion' 
local main_model_eq2 i.Year `demographics_raw' `contextual_control' `religion' i.UnEmpl c.RAT_UnEmpl i.PhysProb c.RAT_PhysProb

local main_model_eq3 i.Year `demographics_same' `contextual_control' `religion' 
local main_model_eq4 i.Year `demographics_same' `contextual_control' `religion' i.UnEmpl c.std_same_prop_UnEmpl i.PhysProb c.std_same_prop_PhysProb

local main_model_eq5 i.Year `demographics_inter' `contextual_control' `religion' 
local main_model_eq6 i.Year `demographics_inter' `contextual_control' `religion' i.UnEmpl##c.std_same_prop_UnEmpl i.PhysProb##c.std_same_prop_PhysProb

* missing pattern analysis 
mi misstable summarize Suic Female White_nh Black_nh AIAN_nh AsPI_nh Hispanic BornUSA Age_15_24 Age_25_44 Age_45_64 Age_65_Up Marrd5 Widow5 Divor5 Separ5 NvMar5 PhysProb UnEmpl 
mi misstable patterns Suic Female White_nh Black_nh AIAN_nh AsPI_nh Hispanic BornUSA Age_15_24 Age_25_44 Age_45_64 Age_65_Up Marrd5 Widow5 Divor5 Separ5 NvMar5 PhysProb UnEmpl 

* Table S1
estimates clear
mi estimate, post: svy: logit Suic i.St `main_model_eq1' 	
estimates store m1 
mi estimate, post: svy: logit Suic i.St `main_model_eq2' 	
estimates store m2 
mi estimate, post: svy: logit Suic i.St `main_model_eq3' 	
estimates store m3 
mi estimate, post: svy: logit Suic i.St `main_model_eq4' 	
estimates store m4 
mi estimate, post: svy: logit Suic i.St `main_model_eq5' 	
estimates store m5 
mi estimate, post: svy: logit Suic i.St `main_model_eq6' 	
estimates store m6 

esttab m1 m2 m3 m4 m5 m6 using "${figure_file}/mi_logit_appendix_table.csv", csv replace star (+ 0.1 * 0.05 ** 0.01 *** 0.001) label nogap se 

log close




